package com.juzi.wopu.web;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import com.juzi.wopu.domain.CartShelfv;
import com.juzi.wopu.query.CartShelfvQuery;
import com.juzi.wopu.query.PageList;
import com.juzi.wopu.service.ICartShelfvService;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;

public class CartShelfvAction extends CRUDAaction<CartShelfv> {
	
	private static final long serialVersionUID = 1L;
	// 注入Service接口
	private ICartShelfvService cartShelfvService;

	private CartShelfv cartShelfv = new CartShelfv();
	// 实例化一个查询对象
	private CartShelfvQuery baseQuery = new CartShelfvQuery();
	// 当前类对象 是栈顶对象
	private PageList pageList;
	
	public void setCartShelfvService(ICartShelfvService cartShelfvService) {
		this.cartShelfvService = cartShelfvService;
	}
	
	@Override
	protected void list() {
		this.pageList = cartShelfvService.findByQuery(baseQuery);
		//putContext("Depts", deptService.getAll());
	}
	
	/**
	 * 上架和下架
	 * @return
	 */
	public String upDownStatus(){
		cartShelfvService.updateStatus(id);
		return RELOAD;
	}

	@Override
	public String input() throws Exception {
		return INPUT;
	}

	// 做非空，长度，格式
	public void validateSave() {
		if (StringUtils.isBlank(cartShelfv.getsProductName())) {
			addFieldError("name", "名称不能为空");
		}
	}

	/**
	 * 处理保存和修改方法
	 */
	@Override
    @InputConfig(methodName="input")
	public String save() throws Exception {
		//  新增后的保存
		if(id==null){
			// 保存后添加到最后一页
			baseQuery.setCurrentPage(Integer.MAX_VALUE);
		}
		// cartShelfvService.saveorUpdate(cartShelfv);
		return RELOAD;
	}

	/**
	 * 删除
	 */
	@Override
	public String delete() throws Exception {
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setContentType("text/json;charset=UTF-8");
		PrintWriter out = response.getWriter();
		try {
			if (id != null) {
				cartShelfvService.delete(id);
				out.print("{\"success\":true,\"msg\":\"删除成功\"}");
			}else{
				out.print("{\"success\":false,\"msg\":\"没有删除成功\"}");
			}
		} catch (Exception e) {
			e.printStackTrace();
			out.print("{\"success\":false,\"msg\":\"异常信息:"+e.getMessage()+"\"}");
		}
		return null;
	}

	// input前置方法
	@Override
	public void prepareInput() throws Exception {
		if (id != null) {// 最主要做回显
			cartShelfv = cartShelfvService.get(id);// 变成栈顶对象
		}
	}

	// save前置方法
	@Override
	public void prepareSave() throws Exception {
		if (id == null) {
			// 新增后的保存
			cartShelfv = new CartShelfv();
		} else {
			cartShelfv = cartShelfvService.get(id);
		}
	}

	// 圧栈后院获取
	@Override
	public CartShelfv getModel() {
		return cartShelfv;
	}

	public PageList getPageList() {
		return pageList;
	}

	public CartShelfvQuery getBaseQuery() {
		return baseQuery;
	}

	public void setBaseQuery(CartShelfvQuery baseQuery) {
		this.baseQuery = baseQuery;
	}
}
